<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\db\Query;
use think\Request;
use cmf\controller\AdminBaseController;
use app\admin\model\SchoolModel;

class SchoolController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name=$this->request->param('name');
        $data=Db::name('school')
            ->where(function (Query $query) use ($name){
                if ($name){
                    $query->where('name','like',"%$name%");
                }
            })
            ->order('id desc')
            ->paginate(2,false,$this->request->param());
        $page=$data->render();
        return view('',['school'=>$data,'page'=>$page]);
    }

    public function add()
    {
        $province=Db::name('area')
            ->where('level',1)
            ->all();
        $this->assign('province',$province);
        return $this->fetch();
    }

    public function addPost()
    {
        $data=input();
        if (empty($data['name'])){
            $this->error('学校名不能为空');
        }
        if (empty($data['address'])){
            $this->error('详细地址不能为空');
        }
        //$add=Db::name('school')->insert(input());
        $add = SchoolModel::create($data);
        if ($add){
            $this->success('添加成功','school/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function getCity(){
        $areaId=input('areaId');
        $city=Db::name('area')
            ->where('parentId',$areaId)
            ->where('level',2)
            ->select();
        $cityArr=[
            "<option value=''>--请选择--</option>"
        ];
        foreach ($city as $v){
            $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        return $cityArr;
    }
    public function getArea(){
        $cityId=input('cityId');
        $area=Db::name('area')
            ->where('parentId',$cityId)
            ->where('level',3)
            ->select();
        $cityArr=[
            "<option value=''>--请选择县区--</option>"
        ];
        foreach ($area as $v){
            $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        return $cityArr;
    }
    public function edit()
    {
        $province=Db::name('area')->where('level',1)->select();
        $area=Db::name('area')->where('level',3)->select();
        $city=Db::name('area')->where('level',2)->select();
        if (request()->isPost()){
            $data=input();
            if (empty($data['name'])){
                $this->error('学校名不能为空');
            }
            $edit=SchoolModel::update($data,['id'=>$data['id']]);
            if ($edit){
                $this->success('修改成功','school/index');
            }else{
                $this->error('修改失败');
                }
        }
        $id=input('id');
        $school=SchoolModel::get($id);
        return $this->fetch('edit',['school'=>$school,
            'province'=>$province,
            'area'=>$area,
            'city'=>$city]);
    }
    public function delete($id)
    {
        $del=SchoolModel::destroy($id);
        if ($del){
            $this->success('删除成功','school/index');
        }else{
            $this->error('删除成功');
        }
    }
}
